package com.game.dongzhao.view;

import com.game.dongzhao.bean.CopyWriter;
import com.game.dongzhao.bean.Player;
import com.game.dongzhao.util.ResolveUtil;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.text.Font;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.game.dongzhao.DongZhaoApplication.logger;

public class AsideView extends MainView {
    public AsideView() throws IOException {
    }

    /**
     * 初始化
     */
    @FXML
    @Override
    public void initialize() throws InterruptedException, IOException {
        /**文案区文本*/
        this.getCopyWriteText().setEditable(false);
        this.getCopyWriteText().setWrapText(true);
        this.getCopyWriteText().setText("");
        // 玩家信息初始化
        this.getPlayerInfo().setEditable(false);
        this.getPlayerInfo().appendText("领队经验值 : " + Player.manageExp + "\n");
        this.getPlayerInfo().appendText("实习经验值 : " + Player.practiceExp + "\n");
        this.getPlayerInfo().appendText("玩家等级 :" + Player.level);
        // 时钟初始化
        this.initMapClock();
        this.initCopyWriteClock();
        // 文案初始化
        this.setCopyWrite(new CopyWriter());
        this.getCopyWrite().getTxt(new File("assert/words/aside/aside.txt"));
        this.getCopyWrite().setAside(this.getCopyWrite().getAllTxt()[0]);
        // 道具勾选禁用
        this.getItem_xjg().setDisable(true);
        this.getItem_mhq().setDisable(true);
        this.getItem_fbdp().setDisable(true);
        this.getItem_hmj().setDisable(true);
        this.getItem_zl().setDisable(true);
        // 图像初始化
        this.getMainImg().setImage(new Image("file:assert/img/main/Map1-1/init1.jpg", false));
        this.getMapImg().setImage(new Image("file:assert/img/main/Map1-1/init2.jpeg", false));
        // 禁止提交输入
        this.getSubmitBtn().setDisable(true);
        logger.log(System.Logger.Level.INFO, "主界面初始渲染完成");
        // 字体大小改变
        this.getPlayerInfo().setFont(new Font(15.0));
        this.getCopyWriteText().setFont(new Font(15.0));
        this.getInputText().setFont(new Font(15.0));
        // 区分于主界面
        this.getGoAsideBtn().setVisible(false);
    }

    /**
     * 流程推进
     *
     * @param event
     */
    @Override
    @FXML
    public void goNext(ActionEvent event) {
        logger.log(System.Logger.Level.INFO, "玩家请求下一步 copyWriteClock : " + this.getCopyWriteClock());
        // 下一步要打出的句子
        String nextString = this.getCopyWrite().getAside()[this.getCopyWriteClock()] + "\n";
        // 切换主图片
        String nextImg = ResolveUtil.switchMainImage(nextString);
        if (nextImg != "" && nextImg.length() != 0) {
            this.getMainImg().setImage(new Image(nextImg));
            // ImageDetailsView.mainImageUrl = nextImg;
            logger.log(System.Logger.Level.INFO, "切换图片到" + nextImg);
        }
        // 加入到文本输出缓冲区
        if (!nextString.equals(null + "\n")) this.getCopyWriteText().appendText(nextString);
        /** 各种类型的文本类型判断与相关动作执行*/
        // 该地图文案已结束
        if (this.getCopyWriteClock() == this.getCopyWrite().getAside().length-1 || this.getCopyWrite().getAside()[this.getCopyWriteClock()+1] == null) {
            logger.log(System.Logger.Level.INFO, "支线文案结束,请玩家关闭支线窗口");
            this.getCopyWriteText().appendText("支线文案结束,请玩家关闭支线窗口\n");
            this.getGoNextBtn().setDisable(true);
            this.getAutoGoNextButton().setDisable(true);
            this.getSubmitBtn().setDisable(true);
        }
        // 问句
        else if (nextString.matches("》.*\\?\n")) {
            logger.log(System.Logger.Level.INFO, "进入问句");
            this.setLastQuestion(nextString);
            // 时钟后移到第一个选项
            this.copyWriteClockRun();
            nextString = this.getCopyWrite().getAside()[this.getCopyWriteClock()] + "\n";
            do {
                // 输出选项
                this.getCopyWriteText().appendText(nextString);
                // 时钟后移
                this.copyWriteClockRun();
                nextString = this.getCopyWrite().getAside()[this.getCopyWriteClock()] + "\n";
            } while (nextString.matches("[a-z]\\..*\n"));
            // 等待返回回答 禁用下一步
            this.getGoNextBtn().setDisable(true);
            this.getSubmitBtn().setDisable(false);
        }
        // 输入动作的提示
        else if (nextString.matches("（.*）\n")) {
            logger.log(System.Logger.Level.INFO, "进入动作句");
            this.setLastQuestion(nextString);
            // 等待返回回答 禁用下一步
            this.getGoNextBtn().setDisable(true);
            this.getSubmitBtn().setDisable(false);
            this.copyWriteClockRun();
        }
        // 正常下一句
        else {
            this.copyWriteClockRun();
        }
        // exp提示
        if (Player.checkPlayerExp().length() != 0) this.getCopyWriteText().appendText(Player.checkPlayerExp());
    }

    /**
     * 提交输入文本并执行处理
     *
     * @param event
     */
    @FXML
    public void submitInput(ActionEvent event) {
        logger.log(System.Logger.Level.INFO, "玩家支线提交输入");
        List<Object> resolveResult = new ArrayList<>();
        // 仅在需要输入情况下
        if (this.getGoNextBtn().isDisable()) {
            // 提交问题与答案
            if (this.getLastQuestion().matches("》.*\\?\n")) {
                resolveResult = ResolveUtil.questionResolve(this.getLastQuestion(), this.getInputText().getText());
            } else if (this.getLastQuestion().matches("（.*）\n")) {
                resolveResult = ResolveUtil.actionResolve(this.getLastQuestion(), this.getInputText().getText());
            } else {
                resolveResult.add(0);
                resolveResult.add("异常，不存在的问题\n");
            }
            // 清除输入 输出结果
            this.getInputText().setText("");
            this.getCopyWriteText().appendText(resolveResult.get(1).toString());
            // 结果正确的话 允许推进
            if ((Boolean) resolveResult.get(0)) {
                // 数据校正 只有？题加经验
                if (this.getLastQuestion().matches("》.*\\?\n")) {
                    Player.increasePracticeExp(10);
                    Player.updateLevel();
                    this.updatePlayerInfo();
                }
                // 切回行动模式
                this.getSubmitBtn().setDisable(true);
                this.getGoNextBtn().setDisable(false);
            } else {
                // 支线玩家答错不扣分
                // Player.declinePracticeExp(10);
                Player.updateLevel();
                this.updatePlayerInfo();
                // 仅问题切回行动模式
                if ((int) resolveResult.get(2) == 0) {
                    this.getSubmitBtn().setDisable(true);
                    this.getGoNextBtn().setDisable(false);
                }
            }
        }
    }
}
